package com.ecarx.vhaltests.hy11.testcases.system.seatadjustment;

import static android.car.VehiclePropertyIds.SEAT_TILT_MOVE;

import android.car.VehicleAreaSeat;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;
import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

//[SW_FC_004_045]第一排座垫折叠调节1stRowSeatCushionTiltAdjustment
public class SeatTIltMoveTest extends VhalUnitTestBase {
    public static final String TAG = "SeatTIltMoveTest";
    private static int halValueRow1Left;
    private static int halStatusRow1Left;
    private static int halValueRow1Right;
    private static int halStatusRow1Right;
    final static Object lockRow1Left = new Object();
    final static Object lockRow1Right = new Object();
    private static final int SEAT_TILT_OFF = 0;
    private static final int SEAT_TILT_UP = 1;
    private static final int SEAT_TILT_DOWN = 2;
    private static final int PARAM_INVALID = 6;
    private static final IpcpClient ipcpClient = new IpcpClient();
    @BeforeClass
    public static void testInit() {
        mCarPropertyMgr.registerCallback(carPropertyEventCallback, SEAT_TILT_MOVE, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig();
    }
    @Test
    public void testSeatTiltMoveNotAvail() {
        setSeatTiltMoveNotAvail();
        lockRow1Left();
        lockRow1Right();
        Assert.assertEquals(halStatusRow1Left, 1);
        Assert.assertEquals(halStatusRow1Right, 1);
        testSeatTiltMoveNotAvailLeft();
        testSeatTiltMoveNotAvailRight();
    }
    @Test
    public void testSeatTiltMoveAvail() {
        setSeatTiltMoveAvail();
        lockRow1Left();
        lockRow1Right();
        Assert.assertEquals(halStatusRow1Left, 0);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveIsAvailLeft();
        testSeatTiltMoveIsAvailRight();
    }

    /**
     *主驾折叠 UP 正常向上中
     */
    @Test
    public void testSeatTiltMoveRow1LeftUp() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_UP);
        setCDMDriver(60, 0, 1, 3);//当前百分比60%，硬按键未按下，座椅折叠正在向上调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_UP);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_UP);
    }

    /**
     * 主驾折叠 UP 正常向上后到最大停止
     */
    @Test
    public void testSeatTiltMoveRow1LeftUpStop() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_UP);
        setCDMDriver(100, 0, 1, 3);//当前百分比100%，硬按键未按下，座椅折叠正在向上调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);//停止向上折叠
    }

    /**
     * 主驾折叠 UP ,已经无法再向上，再设置向上
     */
    @Test
    public void testSeatTiltMoveRow1LeftUpCannotMove() {
        setSeatTiltMoveAvail();
        setCDMDriver(100, 0, 0, 3);//当前百分比100%，硬按键未按下，座椅折叠无移动
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_UP);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);//停止向上打开
    }

    /**
     * 主驾折叠 Down  正常向下中
     */
    @Test
    public void testSeatTiltMoveRow1LeftDown() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_DOWN);
        setCDMDriver(40, 0, 2, 3);//当前百分比40%，硬按键未按下，座椅折叠正在向下调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_DOWN);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_DOWN);
    }

    /**
     * 主驾折叠 Down 正常向下后到最小停止
     */
    @Test
    public void testSeatTiltMoveRow1LeftDownStop() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_DOWN);
        setCDMDriver(0, 0, 2, 3);//当前百分比0%，硬按键未按下，座椅折叠正在向下调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);//停止向下
    }

    /**
     * 主驾折叠 Down ,已经无法再向下，再设置向下
     */
    @Test
    public void testSeatTiltMoveRow1LeftDownCannotMove() {
        setSeatTiltMoveAvail();
        setCDMDriver(0, 0, 0, 3);//当前百分比0%，硬按键未按下，座椅折叠正在向下调整
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_DOWN);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);//停止向下
    }

    /**
     * 主驾折叠 OFF
     */
    @Test
    public void testSeatTiltMoveRow1LeftOff() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);
        setCDMDriver(50, 0, 0, 3);//当前百分比50%，硬按键未按下，座椅折叠无移动
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);
    }


    @Test
    public void testSeatTiltMoveRow1LeftParamInvalid() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);

        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, PARAM_INVALID);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, SEAT_TILT_OFF);
    }

    /**
     *副驾折叠 UP 正常向上中
     */
    @Test
    public void testSeatTiltMoveRow1RightUp() {//正常向上中
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_UP);
        setCDMPass(60, 0, 1, 3);//当前百分比60%，硬按键未按下，座椅折叠正在向上调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_UP);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_UP);
    }

    /**
     *副驾折叠 UP ，正常向上后到最大停止
     */
    @Test
    public void testSeatTiltMoveRow1RightUpStop() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_UP);
        setCDMPass(100, 0, 1, 3);//当前百分比100%，硬按键未按下，座椅折叠正在向上调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);//停止向上打开
    }

    /**
     * 副驾折叠 UP ，已经无法再向上，再设置向上
     */
    @Test
    public void testSeatTiltMoveRow1RightUpCannotMove() {
        setSeatTiltMoveAvail();
        setCDMPass(100, 0, 0, 3);//当前百分比100%，硬按键未按下，座椅折叠无移动
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_UP);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);//停止向上打开
    }

    /**
     * 副驾折叠 DOWN ，正常向下中
     */
    @Test
    public void testSeatTiltMoveRow1RightDown() {//正常向下中
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_DOWN);
        setCDMPass(40, 0, 2, 3);//当前百分比40%，硬按键未按下，座椅折叠正在向下调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_DOWN);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_DOWN);
    }

    /**
     * 副驾折叠 DOWN ，正常向下后到最小停止
     */
    @Test
    public void testSeatTiltMoveRow1RightDownStop() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_DOWN);
        setCDMPass(0, 0, 2, 3);//当前百分比40%，硬按键未按下，座椅折叠正在向下调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);//停止向下
    }

    /**
     * 副驾折叠 DOWN ，已经无法再向下，再设置向下
     */
    @Test
    public void testSeatTiltMoveRow1RightDownCannotMove() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_DOWN);
        setCDMPass(0, 0, 0, 3);//当前百分比0%，硬按键未按下，座椅折叠正在向下调整
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);//停止向下
    }

    /**
     * 副驾折叠 OFF
     */
    @Test
    public void testSeatTiltMoveRow1RightOff() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);
        setCDMPass(50, 0, 0, 3);//当前百分比50%，硬按键未按下，座椅折叠无移动
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);
    }

    @Test
    public void testSeatTiltMoveRow1RightParamInvalid() {
        setSeatTiltMoveAvail();
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);

        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);
        setSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, PARAM_INVALID);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Right, SEAT_TILT_OFF);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatTiltMoveValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, SEAT_TILT_OFF);
    }

    public void setSeatTiltMoveValue(int areaId, int value) {
        mCarPropertyMgr.setIntProperty(SEAT_TILT_MOVE, areaId, value);
    }
    public void testSeatTiltMoveValue(int areaId, int value) {
        int halValue = mCarPropertyMgr.getIntProperty(SEAT_TILT_MOVE, areaId);
        Assert.assertEquals(halValue, value);
    }
    public void testSeatTiltMoveIsAvailLeft() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_MOVE, VehicleAreaSeat.SEAT_ROW_1_LEFT);
        Assert.assertTrue(isAvail);
    }
    public void testSeatTiltMoveIsAvailRight() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_MOVE, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
        Assert.assertTrue(isAvail);
    }
    public void testSeatTiltMoveNotAvailLeft() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_MOVE, VehicleAreaSeat.SEAT_ROW_1_LEFT);
        Assert.assertFalse(isAvail);
    }
    public void testSeatTiltMoveNotAvailRight() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_TILT_MOVE, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
        Assert.assertFalse(isAvail);
    }
    public void lockRow1Left() {
        try {
            synchronized (lockRow1Left) {
                lockRow1Left.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void lockRow1Right() {
        try {
            synchronized (lockRow1Right) {
                lockRow1Right.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private static CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                Log.d(TAG, carPropertyValue.toString());
                int areaId = carPropertyValue.getAreaId();
                if (areaId == VehicleAreaSeat.SEAT_ROW_1_LEFT) {
                    halValueRow1Left = (int) carPropertyValue.getValue();
                    halStatusRow1Left = carPropertyValue.getStatus();
                    synchronized (lockRow1Left) {
                        lockRow1Left.notify();
                    }
                } else if (areaId == VehicleAreaSeat.SEAT_ROW_1_RIGHT) {
                    halValueRow1Right = (int) carPropertyValue.getValue();
                    halStatusRow1Right = carPropertyValue.getStatus();
                    synchronized (lockRow1Right) {
                        lockRow1Right.notify();
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
        @Override
        public void onErrorEvent(int i, int i1) {
        }
    };

    private static void setConfig() {
        int cemSvcId = 37;
        int[] cemOperIds = {999};
        int ccSvcId = 135;
        int[] ccOperIds = {200};
        int cc1000SvcId = 136;
        int[] cc1000OperIds = {200};
        int cdmSvcId = 36;
        int[] cdmOperIds = {999};
        boolean retCem = ipcpClient.setConfig(cemSvcId, cemOperIds);
        Assert.assertTrue(retCem);
        boolean retCc = ipcpClient.setConfig(ccSvcId, ccOperIds);
        Assert.assertTrue(retCc);
        boolean retCdm = ipcpClient.setConfig(cdmSvcId, cdmOperIds);
        Assert.assertTrue(retCdm);
        boolean retCc1000 = ipcpClient.setConfig(cc1000SvcId, cc1000OperIds);
        Assert.assertTrue(retCc1000);
    }
    public void setSeatTiltMoveNotAvail() {
        setCarConfig(5);
        setCarConfig1000(3, 6);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_ABANDON);
    }

    public void setSeatTiltMoveAvail() {
        setCarConfig(2);
        setCarConfig1000(2, 2);
        setCDM(50, 0, 0, 0, 50, 0, 0, 0);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING);
    }

    public void setCEM(int carMode, int usgMode) {
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {carMode, usgMode};
        boolean cemResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        Assert.assertTrue(cemResult);
        delay();
    }

    public void setCarConfig(int cc468) {
        int ccSvcId = 135;
        int ccOperId = 200;
        int[] configs = {468};
        int[] values = {cc468};
        boolean ccResult = ipcpClient.setCarconfigSignal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }

    private void setCarConfig1000(int cc1173, int cc1174) {
        int ccSvcId = 136;
        int ccOperId = 200;
        int[] configs = {1173, 1174};
        int[] values = {cc1173, cc1174};
        boolean ccResult = ipcpClient.setCarconfig1000Signal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }

    public void setCDMDriver(int drvrSeatPosPercSeatPosFrntHeiPerc, int drvrSeatBtnPsd, int drvrSeatSwtStsDrvrSeatSwtHeiFrntSts, int drvrSeatPosPercSeatPosFrntHeiQF) {
        setCDM(drvrSeatPosPercSeatPosFrntHeiPerc, drvrSeatBtnPsd, drvrSeatSwtStsDrvrSeatSwtHeiFrntSts, drvrSeatPosPercSeatPosFrntHeiQF, 0, 0, 0, 0);
    }

    public void setCDMPass(int passSeatPosPercSeatPosFrntHeiPerc, int passSeatBtnPsd, int passSeatSwtSts2PassSeatSwtHeiFrntSts, int passSeatPosPercSeatPosFrntHeiQF) {
        setCDM(0, 0, 0, 0, passSeatPosPercSeatPosFrntHeiPerc, passSeatBtnPsd, passSeatSwtSts2PassSeatSwtHeiFrntSts, passSeatPosPercSeatPosFrntHeiQF);
    }

    public void setCDM(int drvrSeatPosPercSeatPosFrntHeiPerc, int drvrSeatBtnPsd, int drvrSeatSwtStsDrvrSeatSwtHeiFrntSts, int drvrSeatPosPercSeatPosFrntHeiQF, int passSeatPosPercSeatPosFrntHeiPerc, int passSeatBtnPsd, int passSeatSwtSts2PassSeatSwtHeiFrntSts, int passSeatPosPercSeatPosFrntHeiQF) {
        int cdmSvcId = 36;
        int cdmOperId = 999;
        String[] cdms = {"drvrSeatExtAdjAllowd", "drvrSeatPosPercSeatPosFrntHeiPerc", "drvrSeatBtnPsd", "drvrSeatSwtStsDrvrSeatSwtHeiFrntSts", "drvrSeatPosPercSeatPosFrntHeiQF", "passSeatPosPercSeatPosFrntHeiPerc", "passSeatBtnPsd", "passSeatSwtSts2PassSeatSwtHeiFrntSts", "passSeatPosPercSeatPosFrntHeiQF"};
        int[] cdmValues = {1, drvrSeatPosPercSeatPosFrntHeiPerc*10, drvrSeatBtnPsd, drvrSeatSwtStsDrvrSeatSwtHeiFrntSts, drvrSeatPosPercSeatPosFrntHeiQF, passSeatPosPercSeatPosFrntHeiPerc*10, passSeatBtnPsd, passSeatSwtSts2PassSeatSwtHeiFrntSts, passSeatPosPercSeatPosFrntHeiQF};
        boolean cdmResult = ipcpClient.setCDMSignal(cdmSvcId, cdmOperId, cdms, cdmValues);
        Assert.assertTrue(cdmResult);
        delay();
    }
}
